const defaultRasterOption = {
  'type': 'raster',
  'layout': {
    'visibility': 'none'
  }
}
const tk = '70846cce6fbe0ab34599e159521bdb64'
const url_img = `http://t0.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${tk}`
const url_img_a = `http://t0.tianditu.gov.cn/cia_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${tk}`
const url_vec = `http://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${tk}`
const url_vec_a = `http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${tk}`
const url_ter = `http://t0.tianditu.gov.cn/ter_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ter&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${tk}`
const url_ter_a = `http://t0.tianditu.gov.cn/cta_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cta&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${tk}`
// const url_img = process.env.VUE_APP_EGIS_IMG

/**
 * 添加全部egis底图
 * @param {map} map  地图对象
 */
export function addTiles(map) {
  addImg(map, 'egis-img', url_img, url_img_a)
  addImg(map, 'egis-vec', url_vec, url_vec_a)
  addImg(map, 'egis-ter', url_ter, url_ter_a)
}

/**
 * 添加地图服务
 * @param {map} map 地图对象
 * @param {string} id 底图id
 * @param {url} layer_url 底图服务地址
 * @param {url} annotion_url 注记服务地址
 */
function addImg(map, id, layer_url, annotion_url) {
  // img
  const style_layer = Object.assign({
    id: 'raster-' + id,
    source: 'source-' + id
  }, defaultRasterOption)

  map.addSource('source-' + id, {
    type: 'raster',
    tiles: [layer_url],
    tileSize: 256
  })
  const style_annotion = Object.assign({
    id: 'raster-' + id + '-a',
    source: 'source-' + id + '-a'
  }, defaultRasterOption)
  map.addSource('source-' + id + '-a', {
    type: 'raster',
    tiles: [annotion_url],
    tileSize: 256
  })

  map.addLayer(style_layer)
  map.addLayer(style_annotion)
}

function copyOpts() {
  return JSON.parse(JSON.stringify(defaultRasterOption))
}

export function getRasterStyle() {
  return copyOpts()
}
